package org.example.object.notify;
public class Consumer {
    private final Object lock;
    private Object[] buffer;

    public Consumer(Object lock, Object[] buffer) {
        this.lock = lock;
        this.buffer = buffer;
    }

    public void consume() {
        //先获取锁
        synchronized (lock) {
            //若缓冲区中无数据则等待
            while (null == buffer[0]) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            //消费
            System.out.println("消费者消费数据：" + buffer[0]);
            buffer[0] = null;
            //唤醒所有等待的线程
            lock.notifyAll();
        }
    }
}
